﻿// Decompiled with JetBrains decompiler
// Type: XamlEditor.TextBoxAdorner
// Assembly: XamlEditor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 2AF0471B-7BFE-4738-893B-A4F91E243353
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\lib\XamlEditor.dll

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace XamlEditor
{
  public class TextBoxAdorner : Adorner
  {
    private Thumb TopLeft;
    private Thumb Right;
    private Thumb Bottom;
    private Thumb BottomRight;
    private Thumb Rotate;
    private VisualCollection VisualChildren;

    protected override int VisualChildrenCount
    {
      get
      {
        return this.VisualChildren.Count;
      }
    }

    public TextBoxAdorner(TextBox AdornedElement)
      : base((UIElement) AdornedElement)
    {
      this.VisualChildren = new VisualCollection((Visual) this);
      this.TopLeft = new Thumb();
      this.Right = new Thumb();
      this.Bottom = new Thumb();
      this.BottomRight = new Thumb();
      this.Rotate = new Thumb();
      this.BuildAdorners();
      this.ThumbHandler();
    }

    private void ThumbHandler()
    {
      this.TopLeft.DragDelta += new DragDeltaEventHandler(this.TopLeftDragDelta);
      this.Right.DragDelta += new DragDeltaEventHandler(this.RightDragDelta);
      this.Bottom.DragDelta += new DragDeltaEventHandler(this.BottomDragDelta);
      this.BottomRight.DragDelta += new DragDeltaEventHandler(this.BottomRightDragDelta);
      this.Rotate.DragDelta += new DragDeltaEventHandler(this.RotateDragDelta);
    }

    private void BuildAdorners()
    {
      this.BuildAdorner(this.TopLeft, 1);
      this.BuildAdorner(this.Right, 2);
      this.BuildAdorner(this.Bottom, 3);
      this.BuildAdorner(this.BottomRight, 4);
      this.BuildAdorner(this.Rotate, 5);
    }

    private void BuildAdorner(Thumb T, int i)
    {
      T.Height = T.Width = 10.0;
      T.Opacity = 0.8;
      T.Background = (Brush) new SolidColorBrush(Colors.Black);
      if (i == 1)
        T.Style = (Style) this.FindResource((object) "TopLeft");
      else if (i == 2)
        T.Style = (Style) this.FindResource((object) "Right");
      else if (i == 3)
        T.Style = (Style) this.FindResource((object) "Bottom");
      else if (i == 4)
      {
        T.Style = (Style) this.FindResource((object) "BottomRight");
        T.Width = T.Height = 7.0;
      }
      else if (i == 5)
        T.Style = (Style) this.FindResource((object) "Rotate");
      this.VisualChildren.Add((Visual) T);
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      Rect rect = new Rect(this.AdornedElement.DesiredSize);
      TextBox textBox = this.AdornedElement as TextBox;
      SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.Green);
      solidColorBrush.Opacity = 0.3;
      Pen pen = new Pen((Brush) solidColorBrush, 1.5);
      drawingContext.DrawRectangle((Brush) null, pen, new Rect(new Point(0.0, 0.0), new Point(textBox.Width, textBox.Height)));
      drawingContext.DrawLine(pen, new Point(textBox.Width / 2.0, 0.0), new Point(textBox.Width / 2.0, -10.0));
      drawingContext.DrawEllipse((Brush) Brushes.Green, pen, new Point(textBox.Width / 2.0, -9.5), 1.5, 1.5);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      TextBox textBox = this.AdornedElement as TextBox;
      this.TopLeft.Arrange(new Rect(new Point(-20.0, -20.0), new Point(20.0, 20.0)));
      this.Right.Arrange(new Rect(new Point(-20.0, -20.0), new Point(textBox.Width * 2.0 + 20.0, textBox.Height + 20.0)));
      this.Bottom.Arrange(new Rect(new Point(-20.0, -20.0), new Point(textBox.Width + 20.0, textBox.Height * 2.0 + 20.0)));
      this.BottomRight.Arrange(new Rect(new Point(-20.0, -20.0), new Point(textBox.Width * 2.0 + 20.0, textBox.Height * 2.0 + 20.0)));
      this.Rotate.Arrange(new Rect(new Point(-20.0, -20.0), new Point(textBox.Width + 20.0, 0.0)));
      return finalSize;
    }

    protected override Visual GetVisualChild(int index)
    {
      return this.VisualChildren[index];
    }

    private void TopLeftDragDelta(object sender, DragDeltaEventArgs e)
    {
      TextBox textBox = this.AdornedElement as TextBox;
      Point position = Mouse.GetPosition((IInputElement) textBox.Parent);
      if (!(textBox.RenderTransform is RotateTransform))
        textBox.RenderTransform = (Transform) new RotateTransform();
      double num1 = Vector.AngleBetween(new Vector(-1.0, 0.0), new Vector(-textBox.Width, -textBox.Height));
      double angle = ((RotateTransform) textBox.RenderTransform).Angle;
      double num2 = Math.Sqrt(Math.Pow(textBox.Width / 2.0, 2.0) + Math.Pow(textBox.Height / 2.0, 2.0));
      double num3 = num2 * Math.Cos((num1 + angle) * Math.PI / 180.0) - num2 * Math.Cos(num1 * Math.PI / 180.0);
      double num4 = num2 * Math.Sin((num1 + angle) * Math.PI / 180.0) - num2 * Math.Sin(num1 * Math.PI / 180.0);
      Canvas.SetLeft((UIElement) textBox, position.X + num3);
      Canvas.SetTop((UIElement) textBox, position.Y + num4);
    }

    private void RightDragDelta(object sender, DragDeltaEventArgs e)
    {
      TextBox textBox = this.AdornedElement as TextBox;
      if (!(textBox.RenderTransform is RotateTransform))
        textBox.RenderTransform = (Transform) new RotateTransform();
      Point position = Mouse.GetPosition((IInputElement) this.TopLeft);
      textBox.Width = Math.Max(position.X, 0.1);
    }

    private void BottomDragDelta(object sender, DragDeltaEventArgs e)
    {
      (this.AdornedElement as TextBox).Height = Math.Max(Mouse.GetPosition((IInputElement) this.TopLeft).Y, 0.1);
    }

    private void BottomRightDragDelta(object sender, DragDeltaEventArgs e)
    {
      TextBox textBox = this.AdornedElement as TextBox;
      Point position = Mouse.GetPosition((IInputElement) this.TopLeft);
      textBox.Width = Math.Max(position.X, 0.1);
      textBox.Height = Math.Max(position.Y, 0.1);
    }

    private void RotateDragDelta(object sender, DragDeltaEventArgs e)
    {
      TextBox textBox = this.AdornedElement as TextBox;
      Point position = Mouse.GetPosition((IInputElement) textBox.Parent);
      Point point = new Point(Canvas.GetLeft((UIElement) textBox) + textBox.Width / 2.0, Canvas.GetTop((UIElement) textBox) + textBox.Height / 2.0);
      double angle = Vector.AngleBetween(new Vector(0.0, -1.0), new Vector(position.X - point.X, position.Y - point.Y));
      textBox.RenderTransform = (Transform) new RotateTransform(angle, textBox.Width / 2.0, textBox.Height / 2.0);
    }
  }
}
